
// 产品模块
const sequelize = require('../config/operateDB');
const {Sequelize, DataTypes, Op} = require('sequelize');


const Products = sequelize.define('products', {
    id: {
        type: DataTypes.BIGINT,
        allowNull: false,
        primaryKey: true,
        autoIncrement: true,
    },
    // 商品名称
    name: {
        type: DataTypes.TEXT,
        allowNull: false,
    },
    // 商品图片
    imgSrc: {
        type: DataTypes.TEXT,
        allowNull: false,
    },
    // 商品介绍
    detail: {
        type: DataTypes.TEXT,
        allowNull: false,
    },
    // 店铺
    owner: {
        type: DataTypes.TEXT,
        allowNull: false,
    },
    // 分类id
    catagoryId: {
        type: DataTypes.BIGINT,
        allowNull: true,
    },
    // 分类名称
    catagoryName: {
        type: DataTypes.TEXT,
        allowNull: false,
    },
    // 价格
    price: {
        type: DataTypes.FLOAT,
        allowNull: false,
    },
}, {

});


Products.sync();


const productsModel = {
    async getProduct(ctx) {
        const params = ctx.query;
        // const count = await Products.count();
        const {offset = 0, limit = 10, keywords = '', catagoryId } = params;
        const searchCondition = {
            name: {
                [Op.like]: '%' + keywords + '%',
            }
        };

        if (catagoryId){
            searchCondition.catagoryId = catagoryId;
        }

        const { count, rows } = await Products.findAndCountAll(
            {
                where: searchCondition,
                limit: parseInt(limit),
                offset: parseInt(offset),
                raw: true,
            }
        );

        ctx.pageRes({
            limit,
            offset,
            list: rows,
            total: count,
        });
    },

    async addProduct(ctx) {
        const params = ctx.request.body;
        console.log(params);
        const validFlag = this.validProduct(ctx, params);
        if (validFlag) {
            const poducts = await Products.create({
                ...params,
            });

            ctx.success('增加成功！', poducts);
        }
    },
    validProduct(ctx, params) {
        if (!params.name) {
            ctx.errorParams('商品名称不存在！');
            return false;
        } else if (!params.imgSrc) {
            ctx.errorParams('商品图片不存在！');
            return false;
        } else if (!params.detail) {
            ctx.errorParams('商品介绍不存在！');
            return false;
        } else if (!params.owner) {
            ctx.errorParams('商品店铺不存在！');
            return false;
        } else if (!params.price) {
            ctx.errorParams('商品价格不存在！');
            return false;
        } else if (!params.catagoryId || !params.catagoryName) {
            ctx.errorParams('商品分类不存在！');
            return false;
        } else {
            return true;
        }
    },
    async deleteProduct(ctx)  {
        const deleteId = ctx.request.body.id;
        if (!deleteId) {
            ctx.errorParams('id不存在！');
        } else {
            await Products.destroy({
                where: {
                    id: deleteId,
                }
            });

            ctx.success('删除成功！', {
                id: deleteId,
            })
        }
        
    },
    async editProduct(ctx)  {
        const params = ctx.request.body;
        console.log(params.id);
        const editId = params.id;
        if (!editId) {
            ctx.errorParams('id不存在！');
        } else {
            // 
            const poducts = await Products.findOne({
                where: {
                    id: editId
                },
                raw: true,
            });
            await Products.update({
                ...poducts,
                ...params,
            }, {
                where: {
                    id: editId
                },
            })
            
            ctx.success('修改成功', {
                id: editId,
            });
        }
        
    }
    
};


module.exports = {
    productsModel,
    Products
};